import os
from pathlib import Path


def get_current_folder_name(file_path: str) -> str:
    """
    仅获取当前目录的字符串名称
    """
    current_file_path = os.path.abspath(os.path.dirname(file_path))
    current_folder_name = os.path.basename(current_file_path)
    return current_folder_name


def get_parent_dir_path(file_path: str) -> str:
    """
    获取当前文件所在的父目录路径
    """
    current_file_path = os.path.abspath(file_path)
    parent_directory_path = os.path.dirname(current_file_path)
    return parent_directory_path


def find_app_root() -> Path:
    """
    动态查找 'app' 目录的路径，确保无论项目目录如何变化，都能定位到正确的 'app' 根目录。
    """
    current_path = Path(__file__).resolve()
    for parent in current_path.parents:
        if (parent / "app").is_dir():
            return parent / "app"
    raise FileNotFoundError("Could not find 'app' directory in the project structure.")


def get_paginator(total_num: int, page_number: int, page_length: int):
    '''
    description: 获得分页器的区间，需要展示的数据的开始和结束段
    param {int} total_num       总共数据条数
    param {int} page_number     页号
    param {int} page_length     页长
    return {int, int} low, high     开始数据索引，结束数据索引
    '''  
    low = (page_number - 1) * page_length if total_num > (page_number - 1) * page_length else 0
    high = page_number * page_length if total_num > page_number * page_length else total_num if total_num > (
                page_number - 1) * page_length else 0
    return low, high


